//
//  FMAntorDescViewController.m
//  PsychologyFM
//
//  Created by 杜建虎 on 16/1/14.
//  Copyright © 2016年 ZhangLuDu. All rights reserved.
//

#import "FMAntorDescViewController.h"
#import "FMAntorDescModel.h"
#import "PKRequestManager.h"
#import "FMAntorDescTwoModel.h"
#import "FMAntorDescTableViewCell.h"
#import "FMAntorDescViewController.h"
#import "FMDiantaiPlayViewController.h"
@interface FMAntorDescViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong)NSMutableArray *dataArr; // tab 数据
@property (nonatomic, strong)NSMutableArray *dataTop; // 头 数据
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)UIView *topView;  // 表头视图
@property (nonatomic, strong)FMAntorDescViewController *AntorDescVC;
@property (nonatomic, strong)UIImageView *headerImage;
@property (nonatomic, strong)UIImageView *backgroupImage;
@property (nonatomic, strong)UILabel *followLabel;
@property (nonatomic, strong)UILabel *userLaber;
@property (nonatomic, strong)UIImageView *tubiaoImage;
@property (nonatomic, strong)UILabel *titleLabel;
@property (nonatomic, strong)UILabel *seeLabel;

@end

@implementation FMAntorDescViewController



- (void)viewDidLoad {
    _dataArr = [NSMutableArray array];
    _dataTop = [NSMutableArray array];
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor grayColor];
    [self addTabview];
    [self requestData];
}

- (void)addTabview
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 49)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 70;
    _topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight / 4)];
    _topView.backgroundColor = [UIColor whiteColor];
    _tableView.tableHeaderView = _topView;

    // 模糊背景图片
    _backgroupImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, _topView.frame.size.height - 40)];
    [_topView addSubview:_backgroupImage];
    
    
    // 返回按钮
    UIButton *returnButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    returnButton.frame = CGRectMake(kScreenWidth / 36, kScreenHeight / 40, kScreenWidth / 15, kScreenWidth / 15);
    [returnButton setBackgroundImage:[UIImage imageNamed:@"fanhui2"] forState:(UIControlStateNormal)];
    [returnButton addTarget:self action:@selector(returnBack:) forControlEvents:(UIControlEventTouchUpInside)];
    [_topView addSubview:returnButton];
    
    // 关注按钮
    UIButton *followButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    followButton.frame = CGRectMake(kScreenWidth - 70, _backgroupImage.frame.size.height - 29, 50, 20);
    [followButton setBackgroundImage:[UIImage imageNamed:@"guanzhu"] forState:(UIControlStateNormal)];
    [followButton addTarget:self action:@selector(followButton:) forControlEvents:(UIControlEventTouchUpInside)];
    [_topView addSubview:followButton];
    
    
    _headerImage = [[UIImageView alloc] initWithFrame:CGRectMake(15, _backgroupImage.frame.size.height - 40, 30, 30)];
    _headerImage.layer.masksToBounds = YES;
    _headerImage.layer.cornerRadius = 15;
    [_topView addSubview:_headerImage];
    
    
    _followLabel = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth / 2 - 40, _backgroupImage.frame.size.height - 20, kScreenWidth / 5, 15)];
    _followLabel.font = [UIFont systemFontOfSize:12];
    _followLabel.textColor = [UIColor whiteColor];
    [_topView addSubview:_followLabel];
    
    _tubiaoImage = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth / 36, _backgroupImage.frame.size.height + 13, 10, 10)];
    _tubiaoImage.image = [UIImage imageNamed:@"geren"];
    [_topView addSubview:_tubiaoImage];
    
    _userLaber = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth / 36 + _tubiaoImage.frame.size.width + 15, _backgroupImage.frame.size.height + 10, kScreenWidth - 80, 15)];
    _userLaber.font = [UIFont systemFontOfSize:11];
    [_topView addSubview:_userLaber];
    
    
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth / 36 + _tubiaoImage.frame.size.width + 35, _backgroupImage.frame.size.height - 40, kScreenWidth / 5, 15)];
    _titleLabel.font = [UIFont systemFontOfSize:12];
    _titleLabel.textColor = [UIColor whiteColor];
    [_topView addSubview:_titleLabel];
    
    _seeLabel = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth / 36 + _tubiaoImage.frame.size.width + 35, _backgroupImage.frame.size.height - 20, kScreenWidth / 5, 15)];
    _seeLabel.font = [UIFont systemFontOfSize:12];
    _seeLabel.textColor = [UIColor whiteColor];
    [_topView addSubview:_seeLabel];
    
    
    [self.view addSubview:_tableView];
    [_tableView registerClass:[FMAntorDescTableViewCell class] forCellReuseIdentifier:@"FMAntorDescTableViewCell"];
}

#pragma --- 其他点击方法
- (void)returnBack:(UIButton *)button
{
       [self.navigationController popViewControllerAnimated:YES];
}


- (void)followButton:(UIButton *)button
{
    NSLog(@"guanzhu");
}

#pragma --- 请求数据
- (void)requestData
{
    NSLog(@"%@",_idStr);
    [PKRequestManager requestWithUrlString:[NSString stringWithFormat:@"http://yiapi.xinli001.com/fm/diantai-detai.json?key=c0d28ec0954084b4426223366293d190&id=%@",_idStr] parDic:nil method:GET finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        FMAntorDescModel *model = [[FMAntorDescModel alloc] init];
        [model setValuesForKeysWithDictionary:dic[@"data"]];
        [_dataTop addObject:model];
        
#pragma --- topView的数据显示在请求时
         _headerImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:model.user[@"avatar"]]]];
        _backgroupImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:model.gcover]]];
        _followLabel.text = [NSString stringWithFormat:@"关注: %@",model.favnum];
        _userLaber.text = model.content;
        _titleLabel.text = model.title;
        _seeLabel.text = [NSString stringWithFormat:@"收听: %@",model.viewnum];
        
    } error:^(NSError *error) {
    }];
    
    [PKRequestManager requestWithUrlString:[NSString stringWithFormat:@"http://yiapi.xinli001.com/fm/diantai-jiemu-list.json?diantai_id=%@&key=c0d28ec0954084b4426223366293d190&offset=0&limit=20",_idStr] parDic:nil method:GET finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *array = dic[@"data"];
        for (NSDictionary *Dic in array) {
            FMAntorDescTwoModel *model = [[FMAntorDescTwoModel alloc] init];
            [model setValuesForKeysWithDictionary:Dic];
            [_dataArr addObject:model];
        }
        [_tableView reloadData];
    } error:^(NSError *error) {
    }];
}


#pragma --- tableView代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FMAntorDescTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FMAntorDescTableViewCell"];
    FMAntorDescTwoModel *model = _dataArr[indexPath.row];
    cell.model = model;
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *str = [NSString stringWithFormat:@"全部节目: %ld",_dataArr.count];
    return str;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FMDiantaiPlayViewController *playVC = [[FMDiantaiPlayViewController alloc] init];
    FMAntorDescTwoModel *model = _dataArr[indexPath.row];
    playVC.value = model.ID;
    playVC.rootVC = _rootVC;
    [self.navigationController pushViewController:playVC animated:YES];
}
- (void)viewWillDisappear:(BOOL)animated
{
    _rootVC.scrollView.height -= 49;
    [_rootVC.view addSubview:_rootVC.tabbar];
    
    
    [self showTabbar];
}

- (void)showTabbar
{
    self.rootVC.tabbar.hidden = NO;
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = YES;
    self.automaticallyAdjustsScrollViewInsets  = NO;
    
//    _rootVC.scrollView.height += 49;
//    _rootVC.tabbar.hidden = YES;
//    [_rootVC.tabbar removeFromSuperview];
}

@end
